package com.app.service;

import com.app.model.main.Accusation;
import com.app.model.main.Article;
import com.app.model.main.CommentsOfArticle;
import com.app.model.main.CommentsOfComments;
import com.app.model.response.RetStatus;
import com.app.repository.interfaces.AccusationMapper;
import com.app.repository.interfaces.ArticleMapper;
import com.app.repository.interfaces.CommentsOfArticleMapper;
import com.app.repository.interfaces.CommentsOfCommentsMapper;
import com.app.service.push.PushService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.IOException;
import java.util.Map;

/**
 * Created by Lichaojie on 2017/4/7. It's a beautiful day.
 */
@Service
public class AccusationService {

    @Autowired
    private ResponseService responseService;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CommentsOfArticleMapper commentsOfArticleMapper;
    @Autowired
    private CommentsOfCommentsMapper commentsOfCommentsMapper;
    @Autowired
    private AccusationMapper accusationMapper;
    @Autowired
    private PushService pushService;

    public RetStatus doAccusation( String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap = null;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int type = (int)paraMap.get("type");
        int objectId = (int)paraMap.get("objectId");
        int userId = (int)paraMap.get("userId");
        int reason = (int)paraMap.get("reason");
        String time = (String)paraMap.get("time");

        long receiverId = 0l;
        switch(type){
            case 0:
                Article article = articleMapper.selectByPrimaryKey((long)objectId);
                receiverId = article.getUserId();
                break;
            case 1:
                CommentsOfArticle commentsOfArticle = commentsOfArticleMapper.selectByPrimaryKey((long)objectId);
                receiverId = commentsOfArticle.getUserId();
                break;
            case 2:
                CommentsOfComments commentsOfComments = commentsOfCommentsMapper.selectByPrimaryKey((long)objectId);
                receiverId = commentsOfComments.getUserId();
                break;
            case 3:
                receiverId = (long)objectId;
                break;
            default:
                break;
        }

        Accusation accusation = new Accusation();
        accusation.setObjectId((long)objectId)
                .setReceiverId(receiverId)
                .setSenderId((long)userId)
                .setReason((byte)reason)
                .setTime(time)
                .setType((byte)type);

        if(accusationMapper.insertSelective(accusation) == 1){
            pushService.pushAccusation(accusation);
            return responseService.doResponse(100);
        }else{
            return responseService.doResponse(200);
        }
    }
}
